package handlers

import (
	"net/http"
	"strconv"

	"toolsweb-admin/models"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func ListSoftwareCategories(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	var categories []models.SoftwareCategory
	if err := db.Preload("Softwares").Find(&categories).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询失败"})
		return
	}
	c.JSON(http.StatusOK, categories)
}

func CreateSoftwareCategory(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	var category models.SoftwareCategory
	if err := c.ShouldBindJSON(&category); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}

	if err := db.Create(&category).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建失败"})
		return
	}
	c.JSON(http.StatusCreated, category)
}

func UpdateSoftwareCategory(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	id, _ := strconv.Atoi(c.Param("id"))

	var category models.SoftwareCategory
	if err := db.First(&category, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "分类不存在"})
		return
	}

	if err := c.ShouldBindJSON(&category); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}

	db.Save(&category)
	c.JSON(http.StatusOK, category)
}

func DeleteSoftwareCategory(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	id, _ := strconv.Atoi(c.Param("id"))

	if err := db.Delete(&models.SoftwareCategory{}, id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除失败"})
		return
	}
	c.JSON(http.StatusNoContent, nil)
}

func ListSoftwareItems(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	var items []models.SoftwareItem
	if err := db.Find(&items).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询失败"})
		return
	}
	c.JSON(http.StatusOK, items)
}

func CreateSoftwareItem(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	var item models.SoftwareItem
	if err := c.ShouldBindJSON(&item); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}

	if err := db.Create(&item).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建失败"})
		return
	}
	c.JSON(http.StatusCreated, item)
}

func UpdateSoftwareItem(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	id, _ := strconv.Atoi(c.Param("id"))

	var item models.SoftwareItem
	if err := db.First(&item, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "条目不存在"})
		return
	}

	if err := c.ShouldBindJSON(&item); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}

	db.Save(&item)
	c.JSON(http.StatusOK, item)
}

func DeleteSoftwareItem(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	id, _ := strconv.Atoi(c.Param("id"))

	if err := db.Delete(&models.SoftwareItem{}, id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除失败"})
		return
	}
	c.JSON(http.StatusNoContent, nil)
}

func GetCategorySoftwareItems(c *gin.Context) {
	db := c.MustGet("db").(*gorm.DB)
	id, _ := strconv.Atoi(c.Param("id"))

	var items []models.SoftwareItem
	if err := db.Where("software_category_id = ?", id).Find(&items).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "查询失败"})
		return
	}
	c.JSON(http.StatusOK, items)
}
